package chap10collection.test7;

/**
 * @author zdl
 * @date 2024/12/17
 */


import java.util.*;
class Worker{
    String name;
    int age;
    //double salary; //改写为包装类
    Double  salary;
    public Worker(){}
    public Worker(String name, int age, double salary){
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    //hashCode()方法重写错误     salary是double类型  hashCode的返回值是int类型，因此应改写salary类型为Double包装类，然后使用包装类的hashCode方法
    /*int hashCode(){
        return name.hashCode() + age + salary;
    }*/

    @Override
    public int hashCode() {
        return name.hashCode()+age+salary.hashCode();
    }

    public boolean equals(Worker w){
        if (w.name == name && w.salary == salary && w.age == age){
            return true;
        }else return false;
    }
}

public class Demo7 {
    public static void main(String args[]){
        Set set = new HashSet();
        set.add(new Worker("“tom”", 18, 2000));
        set.add(new Worker("“tom”", 18, 2000));
        //set.add(0, new Worker("“jerry”", 18, 2000)); //HashSet 无序，不能使用下标
        System.out.println(set.size());  //2
    }
}
